--
-- Author: shunguo.chen
-- Date: 2016-06-20 12:47:35
--
-- MallView

local TabsBtn = require("app.widget.TabsBtn")
local MallPanel = import(".MallPanel")
local PayPanel = import(".PayPanel")
local LotteryPanel = import(".LotteryPanel")
local GiftPanel = import(".GiftPanel")
local SuperGiftPanel = import(".SuperGiftPanel")
local DailyGift = import(".DailyGift")
local OpenServerGift = import(".OpenServerGift")
local ShopDrawPanel = import(".ShopDrawPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MallScene

function ClassRef:init()
	-- dumpNodeChildren(self._root, "MallView")

	self._ui.Button_close:addClickEventListener(function()
		executeFunc(self._onCloseView, self)
	end)
	-- 客服按钮
	self._ui.Button_service:addClickEventListener(function()
		app.channelAPI:openURL("https://kf.qq.com/touch/scene_product.html?scene_id=kf6454")
	end)
	if app:getInnerPublisher() == Enums.Publisher.tencent then
		self._ui.Button_service:setVisible(true)
	else
		self._ui.Button_service:setVisible(false)
	end
	-- 根据功能开放，初始化tab
	local panel = self._ui.Panel
	local tabConfigs = {}
	local panels = {}
	for i,tabKey in ipairs(Constant.SHOP_ORDER_LIST) do
		local tabPanel, tabName
		if UIHandler:isFunctionOpen(tabKey) then
			if tabKey == Constant.FUNC_TYPE_SHOP then
				tabPanel = MallPanel.create(panel:getChildByName("shop"))
				tabName = L("MallTabMall")
			elseif tabKey == Constant.FUNC_TYPE_LOTTERY then
				tabPanel = LotteryPanel.create(panel:getChildByName("teneven"))
				tabName = L("MallTabLottery")
			elseif tabKey == Constant.FUNC_TYPE_SHOPDRAW then
				-- 奇迹卡片要判断是否有卡池开启
				if app:sendMsg("MallModel", "showDrawIsOpen") then
					tabPanel = ShopDrawPanel.create(panel:getChildByName("draw"))
					tabName = L("MallTabShopDraw")
				end
			elseif tabKey == Constant.FUNC_TYPE_TENCENT_SUPER_GIFT then
				if app:sendMsg("MallModel", "isHaveTencentSuperGift") then
					tabPanel = SuperGiftPanel.create(panel:getChildByName("B_gifts"))
					tabName = L("MallTabSuperGift")
				end
			elseif tabKey == Constant.FUNC_TYPE_TENCENT_GIFT then
				tabPanel = GiftPanel.create(panel:getChildByName("gifts"))
				tabName = L("MallTabEverydayGift")
			elseif tabKey == Constant.FUNC_TYPE_EMA_DAILY_GIFT then
				tabPanel = DailyGift.create(panel:getChildByName("gifts"))
				tabName = L("MallTabEverydayGift")
			elseif tabKey == Constant.FUNC_TYPE_EMA_SERVER_GIFT then
				tabPanel = OpenServerGift.create(panel:getChildByName("4gifts"))
				tabName = L("MallTabOpenServiceGift")
			elseif tabKey == Constant.FUNC_TYPE_RECHARGE then
				tabPanel = PayPanel.create(panel:getChildByName("recharge"))
				tabName = L("MallTabRecharge")
			end
			if tabPanel and tabName then
				table.insert(tabConfigs, {panel=tabPanel, key=tabKey, name=tabName})
				table.insert(panels, tabPanel)
			end
		end
	end

	self._tabConfigs = tabConfigs
	self._panels = panels

	self:arrangeTabBtn()
end

--{panel = payPanel, key = Constant.FUNC_TYPE_RECHARGE, name = L("MallTabRecharge")}
function ClassRef:arrangeTabConfigs(isPlus, keyOfPanel)
	local tabConfigs = self._tabConfigs
	local panels = self._panels
	local isExist = false
	local index
	for k, v in pairs(tabConfigs) do
		if v.key == keyOfPanel then
			isExist = true
			index = k
			break
		end
	end

	if isPlus then	-- 加
--		if isExist then return end
--		table.insert(tabConfigs, config)
	else			-- 减
		if not isExist then return end
		if not index then return end
		table.remove(tabConfigs, index)
		panels[index]:setVisible(false)
		table.remove(panels, index)
	end

	self:arrangeTabBtn()
end

function ClassRef:arrangeTabBtn()
	if self._tabsBtn then self._tabsBtn = nil end

	local defSelected = 1
	local tabConfigs = self._tabConfigs
	local btns = {}
	for i = 1, 7 do
		local btn = self._ui.Panel:findChild("Panel_tab/selected_btn_"..i)
		if btn then
			local tabConfig = tabConfigs[i]
			if tabConfig then
				-- 有腾讯直购礼包时，要求优先选择
				if tabConfig.key == Constant.FUNC_TYPE_TENCENT_GIFT then
					defSelected = i
				end
				btn:setVisible(true)
				local name1 = btn:findChild("name_1")
				if name1 then
					name1:setString(tabConfig.name)
				end

				local name2 = btn:findChild("name_2")
				if name2 then
					name2:setString(tabConfig.name)
				end
				table.insert(btns, btn)
			else
				btn:setVisible(false)
			end
		end
	end

	local function onTabsBtn(index)
		if self._curIdx then
			self._curIdx = index
			self:switchPanel()
		else
			self._curIdx = index
		end
	end
	self._tabsBtn = TabsBtn.new(btns, onTabsBtn, defSelected, self._panels)
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
	local config = self._tabConfigs[self._curIdx]
	if config.key == Constant.FUNC_TYPE_LOTTERY or config.key == Constant.FUNC_TYPE_SHOP then
	    local params = {
	        scene = config.key,
	        sceneSelf = self,
	        getFunc = self.getGuideNodeFunc,
	        nextFunc = self.checkGuide
	    }
	    EM:notify("checkGuide", params)
	end
end
-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 420 or id == 442 then
        -- 选择 十连抽 / 单抽
        local panel = self:getPanelByKey(Constant.FUNC_TYPE_LOTTERY)
        return panel:getGuideNode(id)
    elseif id == 441 then
    	-- 点击抽奖页签
    	local _, index= self:_getTabConfigByKey(Constant.FUNC_TYPE_LOTTERY)
    	return self._tabsBtn:getBtn(index)
    end
end

function ClassRef:show(key, shopIndex)
	self.super.show(self)
	self:switchPanel(key, shopIndex)
end

function ClassRef:getPanel(idx)
	return self._panels[idx]
end

-- tab = funcId
function ClassRef:_getIndexByKey(key)
	if not self._tabConfigs then return end
	local tabConfigs = self._tabConfigs
	for k, v in ipairs(tabConfigs) do
		if v.key == key then
			return k
		end
	end
end

function ClassRef:onOperate(cb)
	self.super.onOperate(self, cb)
	for i,panel in ipairs(self._panels) do
		panel:onOperate(cb)
	end
end

function ClassRef:onSwitchPanel(cb)
	self._onSwitchPanel = cb
end

function ClassRef:switchPanel(key, shopIndex)
	if key then
		self._curIdx = self:_getIndexByKey(key)
		self._tabsBtn:setSelected(self._curIdx)
	end
	local config = self._tabConfigs[self._curIdx]
	executeFunc(self._onSwitchPanel, self._curIdx, shopIndex, config.key)

	self:checkGuide()
end

function ClassRef:getPanelByKey(tabKey)
	local config = self:_getTabConfigByKey(tabKey)
	if config then
		return config.panel
	end
end

function ClassRef:_getTabConfigByKey(tabKey)
	for k, v in pairs(self._tabConfigs) do
		if v.key == tabKey then
			return v, k
		end
	end
end

function ClassRef:getCurPanelKey(index)
	if not index then
		index = self._curIdx
	end
	local config = self._tabConfigs[index]
	if config then
		return config.key
	end
end

-- 只考虑lottery的红点
function ClassRef:updateNew(b)
	local config , index= self:_getTabConfigByKey(Constant.FUNC_TYPE_LOTTERY)
	if config then
		local btn = self._tabsBtn:getBtn(index)
		if btn then
			display.setNewPointVisible(btn:getChildByName("dot"), b)
		end
		config.panel:updateNew(b)
	end
end

function ClassRef:updateCoin(num)
    self._panels[self._curIdx]:updateCoin(num)
end

function ClassRef:updateCrystal(num)
	local panel = self._panels[self._curIdx]
	if panel.updateCrystal then
    	panel:updateCrystal(num)
    end
end

function ClassRef:updateMoneyInfo(index)
	local panel = self._panels[self._curIdx]
	if panel.updateMoneyInfo then
		panel:updateMoneyInfo(index)
	end
end

return ClassRef